package icegps.com.pingdj.activity;


import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import java.util.ArrayList;

import icegps.com.pingdj.R;
import icegps.com.pingdj.adapter.MapInfoAdapter;
import icegps.com.pingdj.bean.KeyValue;
import icegps.com.pingdj.map.bean.MapMeasureInfo;
import icegps.com.pingdj.map.listener.OnInfoAndConfigCallback;
import icegps.com.pingdj.utils.UnitUtils;

import static icegps.com.pingdj.constant.ActionKey.TITLE;
import static icegps.com.pingdj.constant.ActionKey.LOAD_TOKEN;

public class MeasureDataMapActivity extends BaseMapActivity implements View.OnClickListener {
    private ArrayList<KeyValue> menu;
    private MapInfoAdapter mapInfoAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_map;
    }

    @Override
    protected String setTitle() {
        return getIntent().getStringExtra(TITLE);
    }

    @Override
    protected void init() {
        super.init();
        initRecycleview();
        setListener();
    }

    @Override
    protected long getSaveToken() {
        return getIntent().getLongExtra(LOAD_TOKEN, System.currentTimeMillis());
    }

    @Override
    protected long getLoadToken() {
        return getIntent().getLongExtra(LOAD_TOKEN, System.currentTimeMillis());
    }

    private void initRecycleview() {
        initLeftMenu();
        recyclerView1.setLayoutManager(new LinearLayoutManager(this));
        mapInfoAdapter = new MapInfoAdapter(R.layout.item_key_value, menu);
        recyclerView1.setAdapter(mapInfoAdapter);
    }

    private void initLeftMenu() {
        menu = new ArrayList<>();
        menu.add(new KeyValue(MAX_HEIGHT, initValue, heightUnit));
        menu.add(new KeyValue(MIN_HEIGHT, initValue, heightUnit));
        menu.add(new KeyValue(AVE_HEIGHT, initValue, heightUnit));
        menu.add(new KeyValue(CURRENT_HEIGHT, initValue, heightUnit));
        menu.add(new KeyValue(AREA, initValue, areaUnit));
        menu.add(new KeyValue(SOIL_VOLUME, initValue, volumeUnit));

    }


    private void setListener() {
        tvEnlarge.setOnClickListener(this);
        tvNarrow.setOnClickListener(this);
        ivLeftOrRight.setOnClickListener(this);
        mapView.setOnMapMultipleListener(onMapMultipleListener);
        mapManager.setOnHeigthInfoCallback(onHeigthInfoCallback);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapManager.loadTile();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        onHeigthInfoCallback = null;
    }

    @Override
    protected boolean pauseIsSaveBitmap() {
        return false;
    }

    @Override
    protected boolean backIsSaveBitmap() {
        return false;
    }

    OnInfoAndConfigCallback onHeigthInfoCallback = new OnInfoAndConfigCallback() {
        @Override
        public void onInfoAndConfig(MapMeasureInfo measureInfo) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    for (KeyValue keyValue : menu) {
                        if (keyValue.key.equals(MAX_HEIGHT)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getMax());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(MIN_HEIGHT)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getMin());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(AVE_HEIGHT)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getAve());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(SOIL_VOLUME)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getSoilVolume());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(CURRENT_HEIGHT)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getCurrentAltitude());
                            keyValue.value = conversion;
                        }
                        if (keyValue.key.equals(AREA)) {
                            String conversion = UnitUtils.conversion(UnitUtils.HEIGHT, deviceStatus.getHeight(), measureInfo.getArea());
                            keyValue.value = conversion;
                        }
                    }
                    mapInfoAdapter.notifyDataSetChanged();
                }
            });
        }
    };

}
